// pages/my/redinfo.js
import Router from '../../utils/router.js'
import base from '../../config/base.js'
import util from '../../utils/util.js'
import rewardsj from '../../services/rewardsj.js'
import shareInfoJson from '../../utils/hbshare.js'
import redpacket from '../../utils/redpacket.js'
import Record from '../../services/record-stat';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: '',
    activity: '',
    fredbg: base.static + '/redenvelopes/fredbg.jpg',
    fhb_btn: base.static + '/redenvelopes/order_suc_hb_fbtn.png',
    fhb_icon: base.static + '/redenvelopes/fhb_icon.png',
    fsj_icon: base.static + '/redenvelopes/fsj_icon.png',
    fbox_right: base.static + '/redenvelopes/fbox_right.png', 
    config_img: '',
    headimgtempFilePath: '',
    //分享 控制 
    share: {
      isShow: false,
      animate: false
    },
    bonus_num: 0,
    transmit: {//formid 
      style: 'position:absolute;top:0;left:0;width:100%;height:100%;background:red;opacity:0;padding:0;',
      value: ''
    },
    cheap_status: 0,
    // now_bounty: 0,  //提现赏金
    // all_bounty: 0,  //累计赏金
    // all_bonus: 0,    //在路上的赏金
    // self_bonus: 0    //已获得的红包总额
    user_info: {},
    activity: {}

  },
  goGonglue() {
    shareInfoJson.cardInfo.goGonglue();
  },
  openshare: function (e) {
    if (this.data.cheap_status) {
      shareInfoJson.getbonusNum(this);
    }else{
      wx.showToast({
        title: '活动已结束！',
        icon: 'none'
      })
    }
    this.viewRecord();
  },
  gomyredpacket() {
    Router.go('myredpacket');
  },
  gomyreward() {
    Router.go('myreward');
  },
  getreward() {
    let now_bounty = 0.00,
      all_bounty = 0.00,
      all_bonus = 0.00,
      self_bonus = 0.00;
    rewardsj.getBounty({}, res => {
      if (res && res.code == 1) {
        let data = res.data,
          bounty_str = this.data.bounty_str,
          self_bonus_str = this.data.self_bonus_str;
        // if (data.now_bounty != null && data.now_bounty != '' && !isNaN(Number(data.now_bounty))) {
        //   data.now_bounty = util.floatAccDiv(data.now_bounty, 1);
        // }
        // if (data.all_bounty != null && data.all_bounty != '' && !isNaN(Number(data.all_bounty))) {
        //   data.all_bounty = util.floatAccDiv(data.all_bounty, 1);
        // }
        // if (data.all_bonus != null && data.all_bonus != '' && !isNaN(Number(data.all_bonus))) {
        //   data.all_bonus = util.floatAccDiv(data.all_bonus, 1);
        // }
        // if (data.self_bonus != null && data.self_bonus != '' && !isNaN(Number(data.self_bonus))) {
        //   data.self_bonus = util.floatAccAdd(data.self_bonus, 1);
        // }
        if (data.all_bounty > 0) {
          bounty_str = `${data.all_bounty}元赏金已到手`;
        }else{
          bounty_str = '还没有赏金哦';
        }
        if (data.self_bonus > 0) {
          self_bonus_str = `已获得${data.self_bonus}元红包`;
        } else {
          self_bonus_str = '还未获得红包';
        }
        if (data.config_img){
          this.setData({
            config_img: data.config_img
          })
        }else {
          this.setData({
            config_img: shareInfoJson.cardInfo.imageUrl
          })
        }
        this.setData({
          bounty_str,
          all_bounty: data.all_bounty,
          self_bonus_str,
          self_bonus: data.self_bonus,
          bonus_num: data.bonus_num
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.getreward();
    //获取 红包 id
    shareInfoJson.getSendRedEnvelope(this);
    wx.hideShareMenu();
    redpacket.getcheap_goods(this);
    let user_info = wx.getStorageSync('user_info');
    let activity = wx.getStorageSync('activity');
    that.setData({
      user_info: user_info,
      activity: activity,
      userid: wx.getStorageSync('user_info').id,
      store_id: options.store_id,
    })
    wx.downloadFile({
      url: base.static + '/store/logo2.png', 
      success: function(res){
        if(res.statusCode == 200){
          that.setData({
            headimgtempFilePath: res.tempFilePath
          })
        }
      },
    })
  },
  //滚动和点击屏幕记录
  viewRecord: function () {
    let that = this;
    let params = {
      name: "ltxx_buyer",
      page: 'redpacket',
      productid: '',
      userid: that.data.userid,
      storeid: that.data.store_id,
      button_name: 'fahongbao',
      act: 'click',
      env: getApp().globalData.env
    }
    Record.recordstat(params, function () { });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    let shareObj = {
      title: shareInfoJson.cardInfo.titlefn(),
      path: shareInfoJson.cardInfo.pathFn(this) +`&hb_sharePath=myredinfo`,
      imageUrl: that.data.config_img,
      success: function (res) {
        console.log(res, 'success');
      },
      fail: function (res) {
        console.log(res, 'fail');
      },
      complete: function () {
        console.log('complete');
      }
    }
    // if(options.from == 'button'){
    //   shareObj.path = '/pages/my/joinshopman?shareType=1';
    // }
    return shareObj;
  }
})